<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">欢迎注册！</h2>
      
      <div class="register-form">
        <!-- 用户名输入 -->
        <div class="input-item">
          <el-input 
            v-model="username" 
            placeholder="请设置用户名"
            :prefix-icon="User"
            size="large"
          />
        </div>
        
        <!-- 密码输入 -->
        <div class="input-item">
          <el-input 
            v-model="password" 
            type="password" 
            placeholder="请设置登录密码"
            :prefix-icon="Lock"
            show-password
            size="large"
          />
        </div>

        <!-- 确认密码 -->
        <div class="input-item">
          <el-input 
            v-model="confirmPassword" 
            type="password" 
            placeholder="请再次确认登录密码"
            :prefix-icon="Lock"
            show-password
            size="large"
          />
        </div>

        <!-- 手机号 -->
        <div class="input-item">
          <el-input 
            v-model="phone" 
            placeholder="请输入您的手机号"
            :prefix-icon="Iphone"
            size="large"
          />
        </div>
        
        <!-- 验证码 -->
        <div class="input-item verification">
          <el-input 
            v-model="verificationCode" 
            placeholder="请输入验证码"
            :prefix-icon="Key"
            size="large"
          />
          <div id="v_container"></div>
        </div>
        
        <!-- 协议同意 -->
        <div class="agreement">
          <el-checkbox v-model="agreed">
            我已阅读并同意<a href="#" class="agreement-link">《数据标注管理平台服务协议》</a>
          </el-checkbox>
        </div>
        
        <!-- 注册按钮 -->
        <div class="buttons">
          <button class="register-btn" @click="handleRegister">立即注册</button>
        </div>
      </div>
    </div>
    
    <!-- 背景图 -->
    <div class="background">
      <img src="@/assets/background.png" alt="背景">
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Key, Iphone } from '@element-plus/icons-vue'
import { GVerify } from '@/utils/verifyCode'

const router = useRouter()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const phone = ref('')
const verificationCode = ref('')
const verifyCode = ref(null)
const agreed = ref(false)

onMounted(() => {
  // 初始化验证码，使用混合类型
  verifyCode.value = new GVerify({
    id: 'v_container',
    type: 'blend', // blend:数字字母混合类型、number:纯数字、letter:纯字母
    width: 140,
    height: 40
  })
})

const handleRegister = async () => {
  try {
    // 表单验证
    if (!username.value || !password.value || !confirmPassword.value || !phone.value) {
      ElMessage.warning('请填写所有必填项')
      return
    }

    if (password.value !== confirmPassword.value) {
      ElMessage.warning('两次输入的密码不一致')
      return
    }

    if (!agreed.value) {
      ElMessage.warning('请阅读并同意服务协议')
      return
    }

    // 验证码验证
    if (!verificationCode.value) {
      ElMessage.warning('请输入验证码')
      return
    }

    // 验证验证码
    if (!verifyCode.value.validate(verificationCode.value)) {
      ElMessage.error('验证码错误')
      verificationCode.value = ''
      return
    }

    // 处理注册逻辑
    console.log('注册')
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error(error.message || '注册失败')
  }
}

const toLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
/* 复用 login.vue 的基础样式 */
.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  justify-content: center;
  align-items: center;
  position: relative;
}

.login-box {
  width: 360px;
  background: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.title {
  text-align: center;
  font-size: 20px;
  color: #333;
  margin-bottom: 30px;
  font-weight: normal;
}

.input-item {
  margin-bottom: 20px;
}

.verification {
  display: flex;
  gap: 10px;
}

.verification :deep(.el-input) {
  flex: 1;
}

#v_container {
  width: 120px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
}

.agreement {
  margin: 20px 0;
  font-size: 14px;
  color: #666;
}

.agreement-link {
  color: #1890ff;
  text-decoration: none;
  transition: color 0.3s;
}

.agreement-link:hover {
  color: #096dd9;
}

.register-btn {
  width: 100%;
  height: 40px;
  background: #4169e1;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background: #3157d5;
}

/* 输入框样式调整 */
:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1890ff inset;
}

/* 图标颜色 */
:deep(.el-input__prefix-icon),
:deep(.el-input__suffix-icon) {
  color: #1890ff;
}

/* 背景图 */
.background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background-color: #f5f7fa;
}

.background img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.6;
}
</style> 